<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #diffQty="{ record }">
        <!-- {{ (parseInt(record.iqty, 10) || 0) - (parseInt(record.sumOverQty, 10) || 0) }} -->
        {{  (parseInt(record.sumOverQty, 10) || 0) - (parseInt(record.iqty, 10) || 0)  }}
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'mo:task:mesOrderTask:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <a-button type="primary" @click="handletask()" v-auth="'mes:orderTask:ryfp'">
          <Icon icon="simple-line-icons:settings" /> {{ t('人员分配') }}
        </a-button>

        <a-popconfirm
          title="是否确认暂停？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handletaskStatus('5')"
        >
          <a-button type="danger" v-auth="'mes:orderTask:stop'">
            <Icon icon="ant-design:stop-outlined" /> {{ t('暂停') }}
          </a-button>
        </a-popconfirm>

        <a-popconfirm
          title="是否确认打开？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handletaskStatus('2')"
        >
          <a-button type="success" v-auth="'mes:orderTask:open'">
            <Icon icon="ant-design:check-circle-outlined" /> {{ t('打开') }}
          </a-button>
        </a-popconfirm>

        <a-popconfirm
          title="是否确认关闭？"
          ok-text="是"
          cancel-text=" "
          placement="bottom"
          @confirm="handletaskStatus('9')"
        >
          <a-button type="danger" v-auth="'mes:orderTask:close'">
            <Icon icon="ant-design:close-circle-outlined" /> {{ t('关闭') }}
          </a-button>
        </a-popconfirm>

        <a-button type="primary" @click="handBgRegister()" v-auth="'mo:order:scOrder:edit'">
          <Icon icon="simple-line-icons:note" /> {{ t('报工登记') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.scOrder.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BgForm @register="registerBgDrawer" @success="handleSuccess" />
    <BgForm2 @register="registerBg2Drawer" @success="handleSuccess" />
    <taskForm @register="registertaskDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoTaskMesOrderTaskList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { mesOrderTaskDelete, mesOrderTaskListData,mesOrderTaskstopTask,
    mesOrderTaskopenTask,
    mesOrderTaskcloseTask,} from '/@/api/mes/mo/task/mesOrderTask';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import BgForm from './bgForm.vue';
  import BgForm2 from './bgForm2.vue';
  import taskForm from '/@/views/mes/mo/order/scOrder/taskForm.vue';

  const { t } = useI18n('mo.task.mesOrderTask');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('产线任务管理'),
  };
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('计划开始'),
        field: 'planStartDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('计划结束'),
        field: 'planOverDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('订单号'),
        field: 'scOrder.code',
        component: 'Input',
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('工序名称'),
        field: 'basOper.name',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },

      {
        label: t('任务状态'),
        field: 'taskStatus',
        component: 'Select',
        componentProps: {
          dictType: 'mes_task_status',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'scOrder.code',
      key: 'sc.code',
      sorter: true,
      width: 130,
      align: 'left',
      // slot: 'firstColumn',
      fixed: 'left',
    },
    {
      title: t('行号'),
      dataIndex: 'scOrder.sortNum',
      key: 'sc.sort_num',
      sorter: true,
      width: 60,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('机台'),
      dataIndex: 'fp.lineCode',
      // key: 'a.line_code',
      // sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('订单子表ID'),
      dataIndex: 'modid',
      key: 'a.modid',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
      ifShow: false,
      fixed: 'left',
    },
    {
      title: t('工序编码'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工序名称'),
      dataIndex: 'basOper.name',
      key: 'oper.name',
      sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('计划开始'),
      dataIndex: 'planStartDate',
      key: 'a.plan_start_date',
      sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('计划结束'),
      dataIndex: 'planOverDate',
      key: 'a.plan_over_date',
      sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 120,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('计划数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('操作工'),
      dataIndex: 'userNames',
      width: 100,
      align: 'left',
    },
    {
      title: t('计件单价'),
      dataIndex: 'iprice',
      key: 'a.iprice',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('报工数'),
      dataIndex: 'sumOverQty',
      key: 'a.sum_over_qty',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('差异数'),
      key: 'diffQty',
      sorter: true,
      width: 80,
      align: 'right',
      slot: 'diffQty',
    },
    {
      title: t('合格数'),
      dataIndex: 'sumOkQty',
      key: 'a.sum_ok_qty',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('任务状态'),
      dataIndex: 'taskStatus',
      key: 'a.task_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_task_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 120,
    actions: (record: Recordable) => [
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('编辑产线任务'),
      //   onClick: handleForm.bind(this, { id: record.id }),
      //   auth: 'mo:task:mesOrderTask:edit',
      // },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除产线任务'),
      //   popConfirm: {
      //     title: t('是否确认删除产线任务'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'mo:task:mesOrderTask:edit',
      // },
      {
        label: t('报工登记'),
        title: t('报工登记'),
        onClick: handleBgForm.bind(this, { id: record.id }),
        auth: 'mo:task:mesOrderTask:edit',
        ifShow: () => record.taskStatus == '2' || record.taskStatus == '3',
      },
    ],
  };

  const [registertaskDrawer, { openDrawer: opentaskDrawer }] = useDrawer();

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerBgDrawer, { openDrawer: openBgDrawer }] = useDrawer();
  const [registerBg2Drawer, { openDrawer: openBg2Drawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows,getSelectRowKeys }] = useTable({
    api: mesOrderTaskListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleBgForm(record: Recordable) {
    openBg2Drawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await mesOrderTaskDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handletask() {
    if (getSelectRows().length) {
      let arr = await getSelectRows();
      opentaskDrawer(true, { arr });
    } else {
      showMessage('请选择一行数据！！');
    }
  }
  async function handletaskStatus(taskStatus) {
    if (getSelectRowKeys().length) {
      let arr = getSelectRowKeys();
      let selIds = arr.join(',');
      let res = {};
      if (taskStatus == '2') {
        res = await mesOrderTaskopenTask({ selIds, taskStatus });
      }
      if (taskStatus == '5') {
        res = await mesOrderTaskstopTask({ selIds, taskStatus });
      }
      if (taskStatus == '9') {
        res = await mesOrderTaskcloseTask({ selIds, taskStatus });
      }
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 报工登记
  async function handBgRegister() {
    if (getSelectRows().length) {
      openBgDrawer(true, getSelectRows());
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
